package com.example.clsj.anno;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.lang.reflect.Field;
import java.util.StringJoiner;

/**
 * 检测必输字段工具
 *
 * @author cy_
 * @date 2024/11/1
 */
public class CheckNeedFieldsUtil {

    private static final Logger log = LoggerFactory.getLogger(CheckNeedFieldsUtil.class);

    private CheckNeedFieldsUtil() {
    }

    public static Tuple2<Boolean, String> checkUtil(Object needObj) {
        Field[] declaredFields = needObj.getClass().getDeclaredFields();

        StringJoiner joiner = new StringJoiner(",");

        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                Need annotation = field.getAnnotation(Need.class);
                if (annotation == null) {
                    continue;
                }
                String name = annotation.name();
                Object o = field.get(needObj);
                if (StrUtil.isBlankIfStr(o)) {
                    joiner.add(name);
                }
            } catch (IllegalAccessException e) {
                log.error("反射异常: {}", e.getMessage(), e);
                throw new RuntimeException(e);
            } finally {
                field.setAccessible(false);
            }
        }

        return Tuples.of(!"".equals(joiner.toString()), "请输入：" + joiner);
    }
}
